// const fs = require('fs')
const url = require('url')
// const querystring = require('querystring')
const controller = require('./controller')

module.exports = (req, res) => {
	if (req.method === 'GET') {
		// 获取 get 请求的参数: url.parse(req.url, true)
		// console.log(url.parse(req.url, true).query.id)
		if (req.url === '/' || req.url === '/index.html') {
			/* fs.readFile('./index.html', 'utf-8', (err, data) => {
				if (err) {
					res.setHeader('Content-type', 'text/html;charset=utf-8')
					res.write('<h5>404 NOT Found< h5 > ')
					res.end()
					return
				}

				res.write(data)
				res.end()
			}) */
			controller.index(res)
		}
	} else if (req.method === 'POST') {
		let postData = ''
		req.on('data', (chunk) => {
			postData += chunk.toString()
		})
		req.on('end', (err) => {
			if (err) {
				throw err
			}
			controller.user(postData, res)
		})
	}
}
